package com.ztjy.repository.controller;

import com.github.pagehelper.PageInfo;
import com.ztjy.bean.City;
import com.ztjy.bean.Custom;
import com.ztjy.bean.Users;
import com.ztjy.bean.Warehouse;
import com.ztjy.repository.biz.WarehouseBiz;
import com.ztjy.sys.biz.AreaBiz;
import com.ztjy.sys.biz.UsersBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.xml.ws.soap.Addressing;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/rep/ware")
@SessionAttributes("replist")
public class WarehouseController {
    @Autowired
    private WarehouseBiz wbiz;
    @Autowired
    private AreaBiz abiz;
    @Autowired
    private UsersBiz ubiz;

    /**
     * 仓库管理 (只有财务经理(6)能看 uid不赋值)
     * @return
     */
    @RequestMapping("/allWare")
    public String findAllWare(Model model, Warehouse warehouse,@RequestParam(value = "pageno", defaultValue = "1") Integer pageNo, HttpSession session){
        Integer pageSize=6;
        Users user=(Users) session.getAttribute("currUser");
        Integer jobId = user.getJobId();
        if (jobId==6||jobId==1){
            PageInfo<Warehouse> allRep = wbiz.findAllRep(warehouse, pageNo, pageSize);
            model.addAttribute("replist",allRep);
            if(warehouse!=null){
                Integer cityId = warehouse.getCityId();
                City city = abiz.findCityById(cityId);
                warehouse.setCity(city);
            }
            model.addAttribute("warehouse",warehouse);
            return "forward:/storage/storage/storageList.html";
        }else {
            return "";
        }
    }

    /**
     * 仓库浏览
     * @param model
     * @param warehouse
     * @param pageNo
     * @param session
     * @return
     */
    @RequestMapping("/allWareBrowse")
    public String findAllWareBrowse(Model model, Warehouse warehouse,@RequestParam(value = "pageno", defaultValue = "1") Integer pageNo, HttpSession session){
        Integer pageSize=6;
        Users user=(Users) session.getAttribute("currUser");
        Integer jobId = user.getJobId();
        if (jobId==6||jobId==1){
            PageInfo<Warehouse> allRep = wbiz.findAllRep(warehouse, pageNo, pageSize);
            model.addAttribute("replistb",allRep);
            if(warehouse!=null){
                Integer cityId = warehouse.getCityId();
                City city = abiz.findCityById(cityId);
                warehouse.setCity(city);
            }
            model.addAttribute("warehouseb",warehouse);
            return "forward:/storage/storageBrowse/storageBrowse.html";
        }else if(jobId==7||jobId==8){
            warehouse.setUsreId(user.getuId());
            PageInfo<Warehouse> allRep = wbiz.findAllRep(warehouse, pageNo, pageSize);
            model.addAttribute("replistb",allRep);
            if(warehouse!=null){
                Integer cityId = warehouse.getCityId();
                City city = abiz.findCityById(cityId);
                warehouse.setCity(city);
            }
            model.addAttribute("warehouseb",warehouse);
            return "forward:/storage/storageBrowse/storageBrowse.html";
        }else {
            return "";
        }
    }


        /**
         * 添加前的操作 查询财务和仓库的人员 除财务经理
         * @param model
         * @return
         */
    @RequestMapping("/goadd")
    public String goAddWare(Model model){
        List<Users> users = ubiz.findAllRepJobUsers();
        model.addAttribute("cwUsers",users);
        return "forward:/storage/storage/storageAdd.html";
    }

    /**
     * 添加操作
     * @param warehouse
     * @param model
     * @return
     */
    @RequestMapping("/doadd")
    public String doAddWare(Warehouse warehouse,Model model){
        Boolean flag = wbiz.addWare(warehouse);
        PageInfo<Warehouse> replist = (PageInfo<Warehouse>) model.getAttribute("replist");
        Integer pageNum=replist.getPages();
        if (replist.getTotal()%replist.getPageSize()==0){
            pageNum+=1;
        }
        if (flag){
            return "redirect:/rep/ware/allWare?pageno="+pageNum;
        }else{
            model.addAttribute("msg","添加失败！");
            return "forward:/storage/storage/storageAdd.html";
        }
    }

    /**
     * 根据id查找仓库详情
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/detail/{id}")
    public String findWareDetail(Model model, @PathVariable("id") Integer id){
        Warehouse rep = wbiz.findRepById(id);
        model.addAttribute("detailW",rep);
        return "forward:/storage/storage/storageView.html";
    }

    /**
     * 修改前的操作
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/goupdate/{id}/{pid}/{pageno}")
    public String goUpdateWare(Model model, @PathVariable("id") Integer id, @PathVariable("pid") Integer pid,@PathVariable("pageno") Integer pageno){
        Map map=new HashMap();
        map.put("1","可用");
        map.put("2","不可用");
        model.addAttribute("pagenoU",pageno);
        model.addAttribute("maps",map);
        List<City> cities = abiz.findCitiesByPId(pid);
        model.addAttribute("cities",cities);
        List<Users> users = ubiz.findAllRepJobUsers();
        model.addAttribute("cwUsers",users);
        Warehouse rep = wbiz.findRepById(id);
        model.addAttribute("updateW",rep);
        return "forward:/storage/storage/storageUpdate.html";
    }

    /**
     * 修改操作
     * @param warehouse
     * @param model
     * @return
     */
    @RequestMapping("/doupdate")
    public String doUpdateWare(Warehouse warehouse,Model model,Integer pageno){
        Boolean flag = wbiz.updateWare(warehouse);
        if (flag){
            return "redirect:/rep/ware/allWare?pageno="+pageno;
        }else{
            model.addAttribute("msg","修改失败！");
            return "forward:/storage/storage/storageUpdate.html";
        }
    }

    /**
     * 修改状态
     */
    @RequestMapping("/updatestate")
    public String updateState(Model model, Integer id, String changestate,Integer pageno){
        Warehouse warehouse = wbiz.findRepById(id);
        warehouse.setState(changestate);
        Boolean flag = wbiz.updateWare(warehouse);
        return "redirect:/rep/ware/allWare?pageno="+pageno;
    }
}
